using UnityEngine;

public class HealthBar : MonoBehaviour
{
    [SerializeField]
    private HealthSystem healthSystem;
    private Transform barTransform;

    private void Awake()
    {
        barTransform = transform.Find("bar"); // 获取bar的Transform组件
    }

    private void Start()
    {
        healthSystem.OnDamaged += HealthSystem_OnDamaged; // 订阅受伤事件
        UpdateBar();
        UpdateHealthBarVisible();
    }

    private void HealthSystem_OnDamaged(object sender, System.EventArgs e)
    {
        UpdateBar();
        UpdateHealthBarVisible();
    }

    private void UpdateBar()
    {
        barTransform.localScale = new Vector3(healthSystem.GetHealthAmountNormalized(), 1, 1); // 更新血条的缩放比例
    }

    private void UpdateHealthBarVisible()
    {
        if (healthSystem.IsFullHealth())
        {
            gameObject.SetActive(false); // 如果满血，则隐藏血条
        }
        else
        {
            gameObject.SetActive(true); // 如果不满血，则显示血条
        }
    }
}
